package com.sdp.irrigation.excel.field;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.excel.context.AnalysisContext;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.sdp.core.bizc.vo.BusinessContext;
import com.sdp.irrigation.cache.IrrigationCleanCache;
import com.sdp.irrigation.db.irrigation_field.service.IIrrigationFieldService;
import com.sdp.irrigation.entity.IrrigationField;
import com.sdp.right.entity.SysImportTemplateDetail;
import com.sdp.right.excel.importExcel.ImportExcelCallbackService;
import com.sdp.right.excel.importExcel.ImportExcelRowCallbackService;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.bean.copier.CopyOptions;

@Service
public class FieldExcelCallback implements ImportExcelCallbackService, ImportExcelRowCallbackService {

	private final ThreadLocal<Set<String>> threadLocal = ThreadLocal.withInitial(HashSet<String>::new);
	
	@Autowired
	private IIrrigationFieldService fieldService;
	
	@Autowired
	private IrrigationCleanCache irrigationCleanCache;

	@Override
	public BusinessContext execute(HttpServletRequest request, HttpServletResponse response) {
		//threadLocal.get().clear();
		BusinessContext businessContext = new BusinessContext();
		irrigationCleanCache.cleanFieldCache();
		return businessContext;
	}

	@Override
	public <T> void execute(T rowData, List<T> insertList, List<T> updateList,
			List<SysImportTemplateDetail> templateDetailList, AnalysisContext context) {
		IrrigationField field = (IrrigationField) rowData;
		Set<String> fieldNoSet = threadLocal.get();
		fieldNoSet.add(field.getFieldNo());
	}

	@SuppressWarnings("unchecked")
	@Override
	public <T> void executeBatch(List<T> rowDataList, List<T> insertList, List<T> updateList,
			List<SysImportTemplateDetail> templateDetailList, AnalysisContext context) {
		CopyOptions options = CopyOptions.create()
                .setIgnoreNullValue(true);
		Set<String> fieldNoSet = threadLocal.get();
		QueryWrapper<IrrigationField> queryWrapper = new QueryWrapper<>();
		queryWrapper.lambda().in(IrrigationField::getFieldNo, fieldNoSet);
		List<IrrigationField> fieldList = fieldService.list(queryWrapper);
		Map<String, IrrigationField> fieldMap = new HashMap<>();
		for (IrrigationField field : fieldList) {
			fieldMap.put(field.getFieldNo(), field);
		}
		
		for (Object obj : rowDataList) {
			IrrigationField row = (IrrigationField) obj;
			if (fieldMap.containsKey(row.getFieldNo())) {
				BeanUtil.copyProperties(row, fieldMap.get(row.getFieldNo()), options);
				updateList.add((T) fieldMap.get(row.getFieldNo()));
			} else {
				row.setCreateTime(LocalDateTime.now());
				row.setUpdateTime(row.getCreateTime());
				insertList.add((T)row);
			}
		}
		
		//清理
		fieldList.clear();
		fieldList = null;
		fieldMap.clear();
		fieldMap = null;
		fieldNoSet.clear();
	}

}
